package ntu.edu.sg;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.media.MediaPlayer;
import android.view.KeyEvent;
import android.view.MotionEvent;

// a class that handles the logic, and thus providing the state of the game
// at any given time. It also defines the parameters of the game world
// e.g. the speed of the ball, the width of the playing field
public class GameState {

	// screen width and height
	final int _screenWidth = 300;
	final int _screenHeight = 420;

	// The ball
	final int _ballSize = 10;
	// ball X, and Y coordinates
	int _ballX = 100; 	
	int _ballY = 100;
	// ball's velocity, horizontal and vertical speed
	int _ballVelocityX = 3; 	
	int _ballVelocityY = 3;

	//The bats
	final int _batLength = 75;	
	final int _batHeight = 10;
	// speed of the bats, currently defined
	// as 2 variables, for future purpose
	final int _bottomBatSpeed = 3;
	final int _topBatSpeed = 3;
	
	// the bat at the top
	int _topBatX = (_screenWidth/2) - (_batLength / 2);
	
	// the bat at the bottom
	int _bottomBatX = (_screenWidth/2) - (_batLength / 2);
	
	// Y coordinate is fixed, thus final
	final int _topBatY = 20;
	final int _bottomBatY = 400;

	// score
	int _score = 0;
	final MediaPlayer mp;
	
	// constructor, only to initialize the Mediaplayer sound file
	public GameState(Context _context)
	{
	    mp = MediaPlayer.create(_context, R.raw.pong);
	}

	// on Keypressed method, called whenever user presses a button
	// now only detects if the pressed buttons are the keypad
	// left or right
	public boolean keyPressed(int keyCode, KeyEvent msg)
	{
		// if the button pressed is keypad left
		if(keyCode == KeyEvent.KEYCODE_DPAD_LEFT)
		{
			// update bottom bat location
			_bottomBatX -= _bottomBatSpeed;
		}
		// if the button pressed is keypad right
		if(keyCode == KeyEvent.KEYCODE_DPAD_RIGHT)
		{
			// update bottom bat location
			_bottomBatX += _bottomBatSpeed;
		}

		return true;
	}

	// on screentouch method, called whenever user touches the screen
	public boolean screenTouch(MotionEvent event)
	{
		int xTouch;
		int yTouch;

		// if the touch is considered a press down action
		// or a press down and hold/move
        if(event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_MOVE) 
        { 
        	// gets the touch screen coordinates
			xTouch = (int) event.getX(); 
			yTouch = (int) event.getY();
			
			// if touch left side, and 
			// bottom bat is still within screen
		    if (xTouch < 300/2 && _bottomBatX > 0) {
		    	// move bottom bat to left side
		    	_bottomBatX -= _bottomBatSpeed;
		    }else if(xTouch > 300/2 && _bottomBatX+_batLength<_screenWidth){
		    	// move bottom bat to right side
				_bottomBatX += _bottomBatSpeed;
		    }
        }

        return true;
	}

	//The update method, called by the thread 
	public void update(Context aContext) {

		// updating the new ball location
		_ballX += _ballVelocityX;
		_ballY += _ballVelocityY;
	
		// if ball hits the top of screen,
		// or the bottom, resets game
		if(_ballY > _screenHeight || _ballY < 0){
			_ballX = 100; 	
			_ballY = 200;
			_score=0;
		}  
	
		// if ball collides with the sides
		// change direction
		if(_ballX > _screenWidth || _ballX < 0)
			_ballVelocityX *= -1; 	  	
	
		// if ball hits the top bat,
		// change direction, and speed up
		if(_ballX > _topBatX && _ballX < _topBatX+_batLength && _ballY < _topBatY){
			_ballVelocityY *= -1.3;  
			mp.start();
		}
		// if ball hits the bottom bat,
		// change direction, and speed up
		// and also adds 1 to score
		if(_ballX > _bottomBatX && _ballX < _bottomBatX+_batLength && _ballY > _bottomBatY){
			_ballVelocityY *= -1.3;
			_score+=1;	
		    mp.start();
		}
		
		// simplest form of AI
		// top bat always follows the ball position
		// thus top bat will never lose
		_topBatX = _ballX-_batLength/2;
		
	}
	
	//the draw method
	public void draw(Canvas canvas, Paint paint) {
		// Clear the screen
		canvas.drawRGB(20, 20, 20);
		// set the colour
		paint.setARGB(200, 0, 200, 0);
		// draw the ball
		canvas.drawRect(new Rect(_ballX,_ballY,_ballX + _ballSize,_ballY + _ballSize), paint);
		// draw the bats
		canvas.drawRect(new Rect(_topBatX, _topBatY, _topBatX + _batLength, _topBatY + _batHeight), paint); //top bat
		canvas.drawRect(new Rect(_bottomBatX, _bottomBatY, _bottomBatX + _batLength, _bottomBatY + _batHeight), paint); //bottom bat
		
		// draw the score
		canvas.drawText(Integer.toString(_score), 20, 20, paint);
	}

}
